#!/usr/bin/perl
#
# pkill - emulates Solaris pkill(1)
#
# Exit codes:
#   0 - matched
#   1 - not matched
#   255 - other errors
#

use strict;
use warnings;

sub pgrep {
    my $pattern = shift;
    my @pids;
    open(PS, "ps -ocommand,pid -axc|");
    while (<PS>) {
        push @pids, $1 if /$pattern\w*\s*(\d+)/;
    }
    close PS;
    return @pids;
}

my $pattern = shift or die "usage: pgrep [pattern]\n";
my @pids = pgrep $pattern or exit 1;
while (<@pids>) {
    kill 15, $_;
}
close PS;

